package com.blb.service.impl;

import com.blb.mapper.BranchCompanyDao;
import com.blb.pojo.BranchCompany;
import com.blb.pojo.PageEntity;
import com.blb.service.BranchCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class BranchCompanyServiceImpl implements BranchCompanyService {

    @Autowired
    private BranchCompanyDao branchCompanyDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @author： yrh
     * @Description:获取分公司分页数据:先从缓存中获取分公司分页信息，没有则读mysql数据，再将数据写入缓存
     * @return: PageEntity<BranchCompany>
     */
    @Override
    public PageEntity<BranchCompany> findBranchCompanyPage(int current, int size) {
        String key="branchLimit_"+current+""+size;
        ValueOperations <String,PageEntity<BranchCompany>> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey){
            System.out.println("缓存获取数据");
            return operations.get(key);
        }else {
            System.out.println(" 数据库读取数据" );
            int count = branchCompanyDao.selectBranchCompanyCount();
            int pageCount = count % size ==0 ? count / size :count / size +1;
            if (current<=0){
                current=1;
            }
            if (current>pageCount){
                current = pageCount;
            }
            List<BranchCompany> data = branchCompanyDao.selectBranchCompanyPage((current - 1) * size, size);
            PageEntity<BranchCompany> pageEntity = new PageEntity<>(size, count, data, current, pageCount);
            //写入缓存
            operations.set(key,pageEntity,1, TimeUnit.MINUTES);
            return pageEntity;

        }

    }

    /**
     * @author： yrh
     * @Description:获取分公司信息:先从缓存中获取分公司信息，没有则读mysql数据，再将数据写入缓存
     * @return: List<BranchCompany>
     */
    @Override
    public List<BranchCompany> queryAllByLimit() {
        String key="branchAll";
        ValueOperations <String,List<BranchCompany>> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey){
            System.out.println("缓存获取数据" );
          return operations.get(key);
        }else {
            List<BranchCompany> companies = branchCompanyDao.queryAllByLimit();
            System.out.println("数据库获取数据 ");
            //写入缓存
            operations.set(key,companies,1, TimeUnit.MINUTES);
            return companies;
        }

    }

    /**
     * @author： yrh
     * @Description:获取分公司策略:先从缓存中获取分公司，没有则读mysql数据，再将数据写入缓存
    * @return: BranchCompany
     */
    @Override
    public BranchCompany queryById(Integer branchId) {
        String key = "Branch_"+branchId;
        ValueOperations<String,BranchCompany> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey){
            System.out.println("缓存获取数据");
            return operations.get(key);
        }else {
            BranchCompany branchCompany = branchCompanyDao.queryById(branchId);
            //写入缓存
            if (branchCompany!=null){
                operations.set(key,branchCompany,1, TimeUnit.MINUTES);
            }
            return branchCompany;
        }
    }

    @Override
    public List<String> queryLocation() {
        return branchCompanyDao.queryLocation();
    }
}
